package com.rm.questions.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.questions.entity.CoreQuestion;
import com.rm.questions.mapper.CoreQuestionMapper;
import com.rm.questions.service.ICoreQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类Controller类
 */
@Controller
@RequestMapping("/core/question")
public class CoreQuestionController {

    @Autowired
    private ICoreQuestionService coreQuestionService;

    @Autowired
    private CoreQuestionMapper coreQuestionMapper;

    @GetMapping("/list")
    @ResponseBody
    public List<CoreQuestion> list(
            @RequestParam(value="pageNum",required=false) int pageNum,
            @RequestParam(value="pageSize",required=false) int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CoreQuestion> coreCategories = coreQuestionService.selectList(null);
        PageInfo<CoreQuestion> pageInfo = new PageInfo<>(coreCategories);
        List<CoreQuestion> list = pageInfo.getList();
        Long total = pageInfo.getTotal();
        return list;
    }


    @GetMapping("/get/{id}")
    @ResponseBody
    public CoreQuestion get(@PathVariable("id") Long id) {
        return coreQuestionService.getById(id);
    }

    @PostMapping("/add")
    @ResponseBody
    public boolean add(@RequestBody CoreQuestion coreQuestion) {
        coreQuestionService.save(coreQuestion);
        return true;
    }

    @GetMapping("/delete")
    @ResponseBody
    public boolean delete(@RequestParam("id") Long id){
        coreQuestionService.deleteById(id);
        return true;
    }

    @PostMapping("/update")
    @ResponseBody
    public boolean update(@RequestBody CoreQuestion coreQuestion) {
        coreQuestionService.updateById(coreQuestion);
        return true;
    }

}
